/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.beans.CalendarDTO;
import com.google.gson.Gson;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.http.HttpSession;
import model.beans.Consulta;
import model.negocio.ConsultaNegocio;

/**
 *
 * @author Juliane Chaerki
 */
@WebServlet(name = "CalendarJsonServlet", urlPatterns = {"/CalendarJsonServlet"})
public class CalendarJsonServlet extends HttpServlet {
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
  
        HttpSession session = request.getSession();
        int tipoUsu = (Integer) session.getAttribute("tipo");
        int idUsu = (Integer) session.getAttribute("idUsuario");
            
        ConsultaNegocio negocio = new ConsultaNegocio();
        List<Consulta> listaConsulta = negocio.listarConsulta(tipoUsu, idUsu);
        List<CalendarDTO> listaCalendario = new ArrayList();
        
         
        for(Consulta cons : listaConsulta){
            CalendarDTO calendar = new CalendarDTO();
            
            //FORMATAR A DATA PARA UMA STRING YYYY-MM-DD 
            SimpleDateFormat formatoData = new SimpleDateFormat("yyyy-MM-dd"); 
            Date dataF = new Date(); 
            dataF = cons.getData();
          
            String data = formatoData.format(dataF);
            
            //FORMATAR HORA
            //Inicio
            SimpleDateFormat formatoHr = new SimpleDateFormat("hh:mm:ss");
            Date horaF = new Date();
            horaF = cons.getHorario().getHora();
            String horaS = formatoHr.format(horaF);     
            //Fim
            GregorianCalendar gc = new GregorianCalendar();  
            gc.setTime(cons.getHorario().getHora());  
            SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss"); 
            if(cons.getTpCons().getDuracao().equals("30")){
                gc.add(Calendar.MINUTE,30);
            }else{
                gc.add(Calendar.HOUR,1);
            }              
            String horaE = sdf.format(gc.getTime()); 
            
            //CONCATENAR DATAS
            String start = data + " " + horaS;
            String end = data + " " + horaE;
            
            //CONCATENAR TITULO
            String title = cons.getCliente().getNome() + "-" + cons.getTpCons().getNome();
            
            //CONCATENAR URL
            String url = "Controladora?action=editarConsulta&idCons=" + cons.getIdConsulta();
            
            //SETAR                       
            calendar.setId(cons.getIdConsulta());  
            calendar.setTitle(title);
            calendar.setStart(start);
            calendar.setEnd(end);
            calendar.setAllDay(Boolean.FALSE);
            calendar.setUrl(url);
            
            listaCalendario.add(calendar);
            
        }
        
          response.setContentType("application/json");
          response.setCharacterEncoding("UTF-8");
          PrintWriter out = response.getWriter();
          out.write(new Gson().toJson(listaCalendario));
          
        }

    
   

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
